package com.woniuxy.carrental.controller;

import com.woniuxy.carrental.entity.Contract;
import com.woniuxy.carrental.entity.PageBean;
import com.woniuxy.carrental.entity.ResponseResult;
import com.woniuxy.carrental.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 合同功能调用
 */
@RestController
public class ContractController {

    @Autowired
    ContractService contractService;

    //分页查询客户合同
    @GetMapping("/selectContract")
    public ResponseResult<PageBean<Contract>> getContract(Contract contract, int pageIndex, int pageSize) {

        PageBean<Contract> pageBean = new PageBean<>();

        try {
            int i = contractService.selectContractCount();
            List<Contract> contracts = contractService.selectContractPage
                    (pageIndex, pageSize, contract);
            pageBean.setTotalRecord(i);
            pageBean.setPageIndex(pageIndex);
            pageBean.setPageSize(pageSize);
            pageBean.setBeanList(contracts);

            return new ResponseResult<PageBean<Contract>>(pageBean, 200, "查询完成");
        } catch (Exception e) {

            return new ResponseResult<PageBean<Contract>>(pageBean, 404, "查询失败");
        }


    }

    @PutMapping("/updateOverTime")
    public  ResponseResult updateOverTime(@RequestBody Contract contract){


        System.out.println(contract.toString());
        contractService.updateByOverTime(contract);

        return new ResponseResult<PageBean<Contract>>(null, 200, "修改成功");
    }

}
